package opl.ui.adapters;

import java.text.DateFormat;
import java.util.List;

import opl.entities.Fixture;
import opl.ui.R;
import opl.utilities.FileHelper;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

public class FixtureAdapter extends ArrayAdapter<Fixture> {

	private static int fixtureTemplate;	
	private List<Fixture> lstFixture;
	private LayoutInflater lf;
	
	static{
		fixtureTemplate=R.layout.fixture_template;
	}
	
	public FixtureAdapter(Context context, List<Fixture> objects) {
		super(context, fixtureTemplate, objects);		
		lstFixture=objects;
		lf=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {		
		if(convertView==null){
			convertView=lf.inflate(fixtureTemplate, null);
		}
		
		TextView tvTeam1Name=(TextView) convertView.findViewById(R.id.tvTeam1Name);
		TextView tvTeam2Name=(TextView) convertView.findViewById(R.id.tvTeam2Name);
		TextView tvMatchDate=(TextView) convertView.findViewById(R.id.tvMatchDate);
		ImageButton imgTeam1Logo=(ImageButton)convertView.findViewById(R.id.imgTeam1Logo);
		ImageButton imgTeam2Logo=(ImageButton)convertView.findViewById(R.id.imgTeam2Logo);
		
		Fixture fixture=lstFixture.get(position);
		
		tvMatchDate.setText(DateFormat.getDateInstance(DateFormat.MEDIUM).format(fixture.getMatchDate()));
		tvTeam1Name.setText(fixture.getTeam1());
		tvTeam2Name.setText(fixture.getTeam2());
		
		//Works on the assumption that the name of the team is same as the logo file name.Appends _ if there is any space.
		imgTeam1Logo.setImageDrawable(FileHelper.ReadFileFromResurceAsDrawable(fixture.getTeam1(), convertView.getContext()));
		imgTeam2Logo.setImageDrawable(FileHelper.ReadFileFromResurceAsDrawable(fixture.getTeam2(), convertView.getContext()));
		return convertView;
	}
	

}
